<?php

/**
 * This is the model class for table "histo_admin".
 *
 * The followings are the available columns in table 'histo_admin':
 * @property integer $id
 * @property integer $idseliste
 * @property string $model
 * @property string $action
 * @property string $modelid
 * @property string $date
 *
 * The followings are the available model relations:
 * @property Seliste $idseliste0
 */
class HistoAdmin extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'histo_admin';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idseliste, model, action, modelid, date', 'required'),
			array('idseliste, modelid', 'numerical', 'integerOnly'=>true),
			array('model', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, idseliste, model, modelid, action, date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idseliste0' => array(self::BELONGS_TO, 'Seliste', 'idseliste'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'N°',
			'idseliste' => 'Seliste',
			'model' => 'Onglet',
			'modelid' => 'Identifiant',
			'action' => 'Action',
			'date' => 'Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		if ($this->idseliste>0) $criteria->compare('t.idseliste',$this->idseliste,true);
		$criteria->compare('model',$this->model,true);
		$criteria->compare('action',$this->action,true);
		$criteria->compare('modelid',$this->modelid);
		$criteria->compare('date',$this->date,true);

		$criteria->with = array('idseliste0');
		
		$sort = new CSort();
		$sort->attributes = array(
			'id',
			'sseliste'=>array(
				'asc'=>'idseliste0.pseudo asc',
				'desc'=>'idseliste0.pseudo desc',
				'label'=>'Séliste'
			),
			'model',
			'action',
			'modelid',
			'date'
		);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>$sort,
			'pagination'=>array(
					'pageSize'=>40,
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return HistoAdmin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public static function historise($controler,$id,$user) {
		// historisation
		$histo = new HistoAdmin();
		$histo->idseliste = $user;
		$histo->model = $controler->id;
		$histo->date = date('Y-m-d H:i:s');
		$histo->action = $controler->action->id;
		$histo->modelid = $id;
		return $histo->save();
	}
	
}
